unit cmdAddSelectionFromTable;

interface

uses  classes, sysutils
    , dbConn
    , appMappldXmlNode
    , appMappldXmlCommand
    , appMappldTypes
    , appMappldXmlContext
    , appMappldLayer
;

type
    {------------------------------ TCmdAddSelectionFromTable }

    {***************************************************************************
    * TCmdAddSelectionFromTable
    ***************************************************************************}
    TCmdAddSelectionFromTable = class( TMappldXmlCommand )
protected
    procedure   internalProccess(); override;
    end;

implementation

{*******************************************************************************
* internalProccess
*******************************************************************************}
procedure TCmdAddSelectionFromTable.internalProccess();
var
    tableName : string;
    sql       : string;
    dbRes     : TDBResult;
    curCode   : string;
    code      : string;
    layer     : TMappldLayer;
    objId     : cardinal;

begin
    curCode := '';
    layer   := nil;
    dbRes   := nil;

    tableName := FxmlNode.ValueAsString;
    sql := 'select ' + XML_FLD_LAYER_CODE + ', ' + XML_FLD_OBJECT_CODE + ' from ' + tableName;

    try
        if ( 0 <> Foptions.tmpDatabaseConn.QueryOpen( sql, dbRes, true ) ) then
        begin
            Raise Exception.Create( Foptions.tmpDatabaseConn.lastErrorStr );
        end;

        while dbRes.fetch() do
        begin
            code := dbRes.asString(0);
            if ( ( curCode <> code ) or ( layer = nil ) ) then
            begin
                layer := Fcontext.layersByCode[ code ];
                if ( layer = nil ) then
                begin
                    continue;
                end;

                curCode := code;
            end;

            try
                objId := dbRes.asInteger(1);
                layer.selectedObjects[ objId ] := true;
            except
                continue;
            end;
        end;
    finally
        FreeAndNil( dbRes );
    end;
end;

initialization

TMappldXmlCommand.registerCommandByCode( XML_CMD_AddSelectionFromTable, TCmdAddSelectionFromTable );

end.
